package cn.kgc.itrip.service.roomService;

import cn.kgc.itrip.common.DtoUtil;
import cn.kgc.itrip.common.EmptyUtils;
import cn.kgc.itrip.dto.Dto;
import cn.kgc.itrip.mapper.room.HotelRoomMapper;
import cn.kgc.itrip.mapper.itripHotelRoom.ItripHotelRoomMapper;
import cn.kgc.itrip.pojo.ItripHotelRoom;
import cn.kgc.itrip.pojo.ItripImage;
import cn.kgc.itrip.pojo.ItripLabelDic;
import cn.kgc.itrip.vo.ItripImageVO;
import cn.kgc.itrip.vo.ItripLabelDicVO;
import cn.kgc.itrip.vo.hotelroom.ItripHotelRoomVO;
import cn.kgc.itrip.vo.hotelroom.SearchHotelRoomVO;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * @program: love_travel
 * @description: 房间实现类
 * @author: Mr.Wang
 * @create: 2020-03-30 14:02
 **/

public class HotelRoomServiceImpl implements HotelRoomService{

    @Resource
    private HotelRoomMapper hotelRoomMapper;

    @Resource
    private ItripHotelRoomMapper itripHotelRoomMapper;

    /**
     * 查询酒店房间床型列表
     * @return
     */
    @Override
    public Dto findHotelRoombed() {
        //查询房间类型
        List<ItripLabelDic> dicVO=hotelRoomMapper.findHotelRoombed();
        //判断是否为空
        if (EmptyUtils.isEmpty(dicVO)){
            return DtoUtil.returnFail("false","100305");
        }
        //如果不为空
        List<ItripLabelDicVO> itripLabelDicVOS=new ArrayList<>();
        ItripLabelDicVO labelDicVO=null;
        for (ItripLabelDic itripLabelDic:dicVO){
            labelDicVO=new ItripLabelDicVO();
            BeanUtils.copyProperties(itripLabelDic,labelDicVO);
            itripLabelDicVOS.add(labelDicVO);
        }
        //返回dto
        return DtoUtil.returnDataSuccess(itripLabelDicVOS);
    }

    /**
     * 查询酒店房间列表
     * @param roomVO
     * @return
     */
    @Override
    public Dto HotelRoomByHotel(SearchHotelRoomVO roomVO) throws Exception{
        //判断酒店id是否为空
        if (EmptyUtils.isEmpty(roomVO.getHotelId())){
            return DtoUtil.returnFail("false","100303");
        }
        //开房和退房时间不能为空
        if (EmptyUtils.isEmpty(roomVO.getStartDate())||EmptyUtils.isEmpty(roomVO.getEndDate())){
            return DtoUtil.returnFail("false","100303");
        }
        //判断退房时间是否大于开放时间
        if (roomVO.getStartDate().getTime()>roomVO.getEndDate().getTime()){
            return DtoUtil.returnFail("false","100303");
        }
        Map<String,Object> param=new HashMap<>();
        //酒店id
        param.put("hotelId",roomVO.getHotelId());
        //房间类型
        param.put("roomBedTypeId",roomVO.getRoomBedTypeId());
        //是否含早餐
        param.put("isHavingBreakfast",roomVO.getIsHavingBreakfast());
        //付款方式
        param.put("payType",roomVO.getPayType());
        //是否可以预定
        param.put("isBook",roomVO.getIsBook());
        //是否可以取消
        param.put("isCancel",roomVO.getIsCancel());
        //是否即时相应
        param.put("isTimelyResponse",roomVO.getIsTimelyResponse());

        List<ItripHotelRoom> hotelRooms = itripHotelRoomMapper.getItripHotelRoomCountByMap(param);

        List<ItripHotelRoomVO> itripHotelRooms=new ArrayList<>();
        ItripHotelRoomVO roomVO1=null;
        for (ItripHotelRoom itripHotelRoom:hotelRooms){
            roomVO1=new ItripHotelRoomVO();
            BeanUtils.copyProperties(itripHotelRoom,roomVO1);
            itripHotelRooms.add(roomVO1);
        }
        return DtoUtil.returnDataSuccess(itripHotelRooms);
    }

    /**
     * 根据targetId查询酒店房型图片(type=1)
     * @return
     * @param targeId
     */
    @Override
    public Dto findRoomImg(String targeId) {
        //判断
        if (EmptyUtils.isEmpty(targeId)){
            return DtoUtil.returnFail("false","100302");
        }
        List<ItripImage> images=hotelRoomMapper.findRoomImg(targeId);

        List<ItripImageVO> itripImageVOS=new ArrayList<>();
        ItripImageVO imageVO=null;
        for (ItripImage itripImage:images){
            imageVO=new ItripImageVO();
            BeanUtils.copyProperties(itripImage,imageVO);
            itripImageVOS.add(imageVO);
        }
        return DtoUtil.returnDataSuccess(itripImageVOS);
    }
}
